package com.jettech.oa.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jettech.oa.domain.Document;
import com.jettech.oa.domain.WorkFlow;

@Repository("documentDAO")
public class DocumentDAO extends GenericDAOHibernate<Document, Long> {
	@Autowired
	public DocumentDAO(SessionFactory sessionFactory){
		this.setSessionFactory(sessionFactory);
	}
	/**
	 * 根据公文主键列表获得公文列表
	 * @param documentIds
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Document> findDocumentListByIds(List<Long> documentIds) {
		String hql = "select id,name,user.id,user.loginName,user.realName,workFlow.id,workFlow.name,processInstanceId,currentStateName from Document where id in (:documentIds)";
		Query query = this.getSession().createQuery(hql);
		query.setParameterList("documentIds", documentIds);
		List<Object[]> objs = query.list();
		if(objs!=null && objs.size()>0){
			List<Document> list = new ArrayList<Document>();
			for(Object[] obj:objs){
				Document doc = new Document();
				doc.setId((Long)obj[0]);
				doc.setName((String)obj[1]);
				com.jettech.oa.domain.User user = new com.jettech.oa.domain.User();
				user.setId((Long)obj[2]);
				user.setLoginName((String)obj[3]);
				user.setRealName((String)obj[4]);
				doc.setUser(user);
				WorkFlow workFlow = new WorkFlow();
				workFlow.setId((Long)obj[5]);
				workFlow.setName((String)obj[6]);
				doc.setWorkFlow(workFlow);
				doc.setProcessInstanceId((Long)obj[7]);
				doc.setCurrentStateName((String)obj[8]);
				list.add(doc);
			}
			return list;
		}
		return null;
	}
}
